﻿using ElasticSearchRealize.Help;
using ElasticSearchRealize.Model;
using ElasticSearchRealize.QueryProvider;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ElasticSearchRealize
{
    using static ElasticSearchHelper;
    class Program
    {
        static void Main(string[] args)
        {
            {
                Console.WriteLine("【原生使用】Snowflake 生产分布式 id.");
                //Snowflake.Instance.SnowflakesInit(0, 0); //【Snowflake】初始化
                for (int i = 0; i < 5; i++)
                {
                    long id = Snowflake.Instance.NextId(); //生产id
                    System.Console.WriteLine($"=>序号:[{i + 1}],时间:[{DateTime.Now:yyyy-MM-ddTHH:mm:ss.ffff}],id=[{id}]");

                    var dateResult = Snowflake.Instance.ExtractDateTime(id, out DateTime dateTime);
                    System.Console.WriteLine($"=>反推演算时间:[{dateTime:yyyy-MM-ddTHH:mm:ss.ffff}],id=[{id}]");
                }

                Console.WriteLine($"\n【扩展使用】Snowflake 生产分布式 id.扩展业务前缀和随机串.");
                for (int i = 0; i < 5; i++)
                {
                    string id = IdWorker.Instance.GetSnowflakeId("Order", 1, 0, false); //生产id
                    System.Console.WriteLine($"=>序号:[{i + 1}],时间:[{DateTime.Now:yyyy-MM-ddTHH:mm:ss.ffff}],id=[{id}]");
                }
            }
            {
                //根据日期自动分索引 库amiao
                var testUserIndexQuery = new QueryUserIndexProvider();
                //testUserIndexQuery.AddDocument(new UserIndex()
                //{
                //    Id = Snowflake.Instance.NextId(),
                //    UserName = "严浩淼",
                //    Age = 15,
                //    Email = "527758393@qq.com",
                //    Phone = "17620372030",
                //    CreateDateTime = DateTime.Now
                //});
                //var data = DateTime.Now.AddMonths(-1);
                //testUserIndexQuery.AddDocument(new UserIndex()
                //{
                //    Id = Snowflake.Instance.NextId(data),
                //    UserName = "严浩淼",
                //    Email = "我是email，乒乓球拍卖完了",
                //    Phone = "17620382030",
                //    CreateDateTime = data
                //});
            }
            {
                //查询索引条数
                //var sql = "select count(1) as QueryCount FROM  \"product_specification_detail\"";
                //IEnumerable<Model.QueryResult> resutCount = ExecuteSQLAsync<Model.QueryResult>(sql).Result;
                //Console.WriteLine($"索引数据条数{resutCount.Sum(x => x.QueryCount)}");
            }
            {
                ///处理模糊查询性能
                //var sql = "select count(1) as QueryCount FROM  \"product_specification_detail\" where descriptiondetail Like '%电源%'";
                //var resutCount = ExecuteSQLAsync<Model.QueryResult>(sql).Result;
                //Console.WriteLine($"模糊搜索索引数据条数{resutCount.Sum(x => x.QueryCount)}");
            }
            {
                ///用于查询大批量数据性能测试
                //var sql = "SELECT pn,descriptiondetail,description from \"product_specification_detail\" where  id <10334179 limit 10000";
                //var resutCount = ExecuteSQLAsync<ProductResult>(sql).Result;
                //Console.WriteLine($"搜索命中索引数据条数{resutCount.Count()}");
            }
            {
                ///用于多索引搜索 库244
                //var ids = new long[] { 4090917244844900400,4079804035186557000};
                //var indexNames = new List<string>() { };
                //foreach (var id in ids)
                //{
                //    var dateResult = Snowflake.Instance.ExtractDateTime(id, out DateTime dateTime);
                //    if (dateResult) indexNames.Add(UserIndex.GetIndexNameByDate(dateTime));
                //}
                //var sql = $"SELECT Id,Phone,Email,CreateDateTime from \"{string.Join(',', indexNames.Distinct())}\"";
                //var resut = ExecuteSQLAsync<UserIndex>(sql).Result;
                //Console.WriteLine($"搜索命中索引数据条数{resut.Count()}");
            }
        }
    }
}
